<?php

/**
 * Description of Autoload
 *
 * @author harriagada
 */
class Loader {

    var $errorFullPaths = array();
    var $meta = array();
    var $title;
    var $stylesheet;
    var $javascript;
    var $layout;
    var $databaseEnabled;
    var $sessionEnabled;
    var $libsAutoload;
    var $validateEnabled;
    var $validateAjax;
    var $validateForms;
    var $encryptionGet;
    var $encryptionPost;
    var $libsPath;
    var $databaseObject;
    var $HTMLTags;
    var $encryptionGetObject;
    var $encryptionPostObject;

    public static function IncludeAllClass($libsPath) {

        if (is_dir($libsPath)) {
            if ($dh = opendir($libsPath)) {
                while (($file = readdir($dh)) !== false) {
                    if (is_dir($libsPath . $file) && $file != "." && $file != ".." && $file != "core" && $file != "loader") {
                        Loader::IncludeAllClass($libsPath . $file . "/");
                    }
                    if (is_file($libsPath . $file)) {
                        Loader::IncludePath($libsPath . $file);
                    }
                }
                closedir($dh);
            }
        }
    }

    public static function IncludePath($path) {
        require_once $path;
    }

    public function LayoutConnect() {
        $includePageTitle = $this->HTMLTags['TITLE']['title'];
        $includeMetaTags = $this->IncludeMetaTags();
        $includeStyleSheet = $this->IncludeStyleSheet();
        $includeJavaScript = $this->IncludeJavaScript();
        $includeAjax = $this->IncludeAjax();

        $this->LoadFrontController();
        require_once(dirname(__FILE__) . "/../../layouts/" . $this->layout . ".php");
    }

    public function LoadFrontController() {

        if (!isset($_GET['id']) || empty($_GET['id'])) {
            $getID = $this->encryptionGetObject->Base64Encode("home");
            $getID = urldecode($getID);
            $this->Redirect("http://rutasutem", "?id=$getID");
        } else {
            $getID = $this->encryptionGetObject->Base64Decode($_GET['id']);
        }
        
        /*
          $requestUri = str_replace("/", "", $_SERVER["REQUEST_URI"]);
          $requestUri = urldecode($requestUri);

          $getID = $this->encryptionGetObject->Decrypt($requestUri);
          echo $getID; */

        /* if(!isset($_GET['id'])){

          }
          $getID = $this->encryptionGetObject->Encrypt($_GET['id']); */
    }

    public function IncludeStyleSheet() {
        return $this->IncludeGeneric($this->HTMLTags['CSS']);
    }

    public function IncludeJavaScript() {
        return $this->IncludeGeneric($this->HTMLTags['JS']);
    }

    public function IncludeAjax() {
        return $this->IncludeGeneric($this->HTMLTags['AJAX']);
    }

    public function IncludeMetaTags() {
        return $this->IncludeGeneric($this->HTMLTags['META']);
    }

    public function IncludeGeneric($HTML) {
        $stringHTML = "";
        foreach ($HTML as $html) {
            $stringHTML.=$html . "\n\t\t";
        }
        return "$stringHTML\n";
    }

    public function Redirect($url, $params = "") {
        header("Location:$url/$params");
    }

}

?>
